package com.lh.api.controller.order;

import com.lh.api.annotation.Login;
import com.lh.api.controller.AbstractController;
import com.lh.api.form.AddExpressForm;
import com.lh.api.form.AfterSaleForm;
import com.lh.api.form.RefundForm;
import com.lh.api.service.OrderAfterSaleService;
import com.lh.api.vo.AfterSaleDetailVo;
import com.lh.api.vo.AfterSaleListVo;
import com.lh.api.vo.ReasonListVo;
import com.lh.common.enums.ReasonEnum;
import com.lh.common.utils.PageUtils;
import com.lh.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gze
 * @ClassName: AfterSaleOrderAction
 * @Description:
 * @date: 2020-05-18 15:21
 */

@Api(value="AfterSaleOrderAction",tags={"退款/售后订单相关接口"})
@RestController
@RequestMapping("/api/afterSale")
public class AfterSaleOrderAction extends AbstractController {

    @Autowired
    private OrderAfterSaleService orderAfterSaleService;

    @ApiOperation(value = "申请退款")
    @PostMapping("/createRefund")
    @Login
    public Result<String> createRefund(@RequestBody @Validated RefundForm refundForm){
        orderAfterSaleService.createRefund(refundForm, getUserId());
        return Result.ok();
    }

    @ApiOperation(value = "申请售后")
    @PostMapping("/createAfterSale")
    @Login
    public Result<String> createAfterSale(@RequestBody @Validated AfterSaleForm afterSaleForm){
        orderAfterSaleService.createAfterSale(afterSaleForm,getUserId());
        return Result.ok();
    }

    @ApiOperation(value = "退款/售后列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class)
    })
    @GetMapping("/getAfterSaleList")
    @ResponseBody
    @Login
    public Result<AfterSaleListVo> getRefundList(Integer pageNum, Integer pageSize){
        Map<String, Object> params = new HashMap<>();
        params.put("userId", getUserId());
        PageUtils<AfterSaleListVo> result = orderAfterSaleService.getList(params, pageNum, pageSize);
        return Result.ok(result);
    }

    @ApiOperation(value = "退款/退货原因查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "reasonType", value = "原因类型：REFUND:申请退款原因; AFTERSALES：申请售后原因; REFUSEREFUND：拒绝退款原因; WAREHOUS：拒绝入库原因", required = true,dataTypeClass= ReasonEnum.class)
    })
    @GetMapping("/getReasonList")
    @ResponseBody
    public Result<List<ReasonListVo>> getReasonList(ReasonEnum reasonType){
        List<ReasonListVo> result = orderAfterSaleService.getReasonList(reasonType);
        return Result.ok(result);
    }


    @ApiOperation(value = "退款/售后详情查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "afterSaleId", value = "退款/售后订单号", required = true,dataTypeClass= String.class)
    })
    @GetMapping("/getAfterSaleDetail")
    @ResponseBody
    @Login
    public Result<AfterSaleDetailVo> getAfterSaleDetail(@RequestParam String afterSaleId){
        AfterSaleDetailVo afterSaleDetailVo = orderAfterSaleService.getAfterSaleDetail(afterSaleId, getUserId());
        return Result.ok(afterSaleDetailVo);
    }

    @ApiOperation(value = "填写物流单")
    @PostMapping("/fillLogistics")
    @Login
    public Result<String> fillLogistics(@RequestBody @Validated AddExpressForm addExpressForm){

        orderAfterSaleService.fillLogistics(addExpressForm, getUserId());
        return Result.ok();
    }


    @ApiOperation(value = "删除退款/售后")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "afterSaleId", value = "退款/售后id", required = true,dataTypeClass= String.class)
    })
    @GetMapping(value = "/delAfterSale")
    @ResponseBody
    @Login
    public Result<String> delAfterSale(String afterSaleId){
        orderAfterSaleService.delAfterSale(getUserId(), afterSaleId);
        return Result.ok();
    }
}
